﻿using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Metadata;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Data.Services.Common;
using System.Runtime.Serialization;

namespace Sustainalytics.DynamicsCRMGateway.Entities
{
    [DataContractAttribute(IsReference = true)]
    [EntityLogicalNameAttribute("new_new_feature_new_claim")]
    [DataServiceKeyAttribute("Id")]
    [EntityAttribute("new_new_feature_new_claimSet")]
    public class FeatureClaimBridge : CrmEntity
    {
        public FeatureClaimBridge() :
            base(EntityLogicalName)
        {
        }

        public const string EntityLogicalName = "new_new_feature_new_claim";

        public const int EntityTypeCode = 10029;

        [AttributeLogicalNameAttribute("new_claimid")]
        public Nullable<Guid> ClaimId
        {
            get { return this.GetAttributeValue<Nullable<Guid>>("new_claimid"); }
            set { this.SetAttributeValue<Nullable<Guid>>("new_claimid", "new_claimid", value); }
        }

        [AttributeLogicalNameAttribute("new_featureid")]
        public Nullable<Guid> FeatureId
        {
            get { return this.GetAttributeValue<Nullable<Guid>>("new_featureid"); }
            set { this.SetAttributeValue<Nullable<Guid>>("new_featureid", "new_featureid", value); }
        }

        [AttributeLogicalNameAttribute("new_new_feature_new_claimid")]
        public Nullable<Guid> FeatureClaimBridgeId
        {
            get { return this.GetAttributeValue<Nullable<Guid>>("new_new_feature_new_claimid"); }
        }

        [AttributeLogicalNameAttribute("new_new_feature_new_claimid")]
        public override Guid Id
        {
            get { return base.Id; }
            set { base.Id = value; }
        }

        [RelationshipSchemaNameAttribute("new_new_feature_new_claim")]
        public IEnumerable<Feature> Features
        {
            get { return this.GetRelatedEntities<Feature>("new_new_feature_new_claim", null); }
            set { this.SetRelatedEntities<Feature>("new_new_feature_new_claim1", "new_new_feature_new_claim", null, value); }
        }

        /// <summary>
        /// Constructor for populating via LINQ queries given a LINQ anonymous type
        /// <param name="anonymousType">LINQ anonymous type.</param>
        /// </summary>
        public FeatureClaimBridge(object anonymousType) :
            this()
        {
            foreach (var p in anonymousType.GetType().GetProperties())
            {
                var value = p.GetValue(anonymousType, null);
                if (p.PropertyType == typeof(Guid))
                {
                    // Type is Guid, must be Id
                    base.Id = (Guid)value;
                    Attributes["new_new_feature_new_claimid"] = base.Id;
                }
                else if (p.Name == "FormattedValues")
                {
                    // Add Support for FormattedValues
                    FormattedValues.AddRange((FormattedValueCollection)value);
                }
                else
                {
                    Attributes[p.Name.ToLowerInvariant()] = value;
                }
            }
        }
    }
}